﻿Imports Aricie.DNN.Modules.LuceneSearch.ModuleProviders
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Search
Imports DotNetNuke.Common.Utilities
Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports System.Linq
Imports ZLDNN.Modules.DNNArticle
Imports DotNetNuke.Entities.Modules.Definitions

Namespace Aricie.DNN.Modules.LuceneSearch.ModuleProviders

    Public Class DNNArticleProvider
        Implements ILuceneSearchableUpgrade
        Implements ISearchable

        Public Sub UpgradeSearchItem(ByVal portalId As Integer, ByRef searchItem As Aricie.DNN.Modules.LuceneSearch.Business.LuceneSearchItemInfo) Implements Aricie.DNN.Modules.LuceneSearch.ModuleProviders.ILuceneSearchableUpgrade.UpgradeSearchItem

            Dim ArticleId As Integer = -1
            If Integer.TryParse(searchItem.SearchKey, ArticleId) Then

                Dim DAC As New DNNArticleController
                Dim DAInfo As DNNArticleInfo = DAC.GetDNNArticleByItemID(ArticleId)

                If (Not String.IsNullOrEmpty(DAInfo.Tags)) Then
                    For Each t As String In DAInfo.Tags.Split(New String() {","}, StringSplitOptions.RemoveEmptyEntries)
                        searchItem.ContentTaxonomy.Add(t.Trim())
                    Next
                End If

                Dim TC As New TagController()
                For Each ti As TagInfo In TC.GetArticleTags(ArticleId)
                    searchItem.ContentTaxonomy.Add(ti.Tag.Trim())
                Next

                Dim AACC As New ArticleAssignedCategoriesController()
                For Each c As CategoryInfo In AACC.GetArticleCategoriesByArticleID(ArticleId)
                    searchItem.ContentTaxonomy.Add(c.Title.Trim())
                Next



                ' on remplace le tabid par celui de visualisation de l'article si possible
                If Aricie.DNN.Services.NukeHelper.DnnVersion.Major > 4 Then
                    Try
                        'TODO: accéder directement à cet élément quand on compilera contre une V6 de DNN
                        Dim HT As Hashtable = DotNetNuke.Framework.Reflection.GetProperty(GetType(ModuleInfo), "ModuleSettings", searchItem.ModuleInfo)
                        searchItem.TabId = Integer.Parse(HT("ViewTab"))
                        ' try...catch sans fallback, si on rate ça on a pas vraiment d'autre solution
                    Catch ex As Exception
                    End Try
                End If

                ' on vire le moduleId qui est incorrect puisqu'il s'agit de l'admin
                searchItem.ModuleId = 0

                If (Not String.IsNullOrEmpty(DAInfo.ViewRoles)) Then
                    Dim rc As New DotNetNuke.Security.Roles.RoleController()
                    For Each Role As String In DAInfo.ViewRoles.Split(New String() {";"}, StringSplitOptions.RemoveEmptyEntries)
                        searchItem.ItemViewRoleIds.Add(rc.GetRoleByName(searchItem.PortalId, Role).RoleID)
                    Next
                Else
                    ' si aucun droit sur cet article, tout le monde peut le voir
                    searchItem.ItemViewRoleIds.Add(-1)
                End If
                ' on ne veut jamais hériter du module
                searchItem.InheritModuleViewPermissions = False

                searchItem.ItemType = "Article"
                ' on écrase le titre puisque par défaut il contient le titre du module
                searchItem.Title = DAInfo.Title

                ' on indique qu'on indexe pas cet élément s'il n'est pas approuvé, ou si la date d'indexation sort de ses dates de publication
                ' Cet élément ne doit pas être affiché dans les conditions suivantes
                searchItem.CanBeReturnedBySearch = DAInfo.IsApproved
                searchItem.ReturnedBySearchWindowStart = DAInfo.PublishDate
                If (DAInfo.ExpireDate <> New Date(1, 1, 1)) Then
                    searchItem.ReturnedBySearchWindowEnd = DAInfo.ExpireDate
                End If


                '    If Not (DAInfo.IsApproved AndAlso DAInfo.PublishDate <= Now AndAlso (DAInfo.ExpireDate > Now OrElse DAInfo.ExpireDate = New Date(1, 1, 1))) Then
                '    searchItem.IndexingStatus = IndexingItemStatus.Skipped
                'End If
            End If

        End Sub

        Public Function GetSearchItems(ModInfo As DotNetNuke.Entities.Modules.ModuleInfo) As DotNetNuke.Services.Search.SearchItemInfoCollection Implements DotNetNuke.Entities.Modules.ISearchable.GetSearchItems
            Dim results As SearchItemInfoCollection = Nothing
            Try
                If (Not String.IsNullOrEmpty(ModInfo.BusinessControllerClass)) Then
                    Dim SearchableController As ISearchable = TryCast(Aricie.Services.ReflectionHelper.CreateObject(ModInfo.BusinessControllerClass), ISearchable)
                    If SearchableController IsNot Nothing Then
                        results = New SearchItemInfoCollection
                        results.AddRange((From el As SearchItemInfo In SearchableController.GetSearchItems(ModInfo) Select el).ToArray)
                    End If
                End If
            Catch ex As Exception
                ' what to do...?
                ' en attendant renvoyons une liste vide
                results = New SearchItemInfoCollection
            End Try
            Return results
        End Function
    End Class

End Namespace